#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int _ = 1e3 + 10;
int n, a[_], b[_], cat[_], dis[_][_], dp[_][_];
int main() {
    freopen("zha.in", "r", stdin);
    freopen("zha.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> b[i];
    }
    cat[1] = 1;
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j < i; j++) {
            cat[i] = (cat[i] + (long long) cat[j] * cat[i-j]) % mod;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == 1 || j == 1) {
                dis[i][j] = 0;
            } else if (a[i-1] == b[j-1]) {
                dis[i][j] = 1;
            } else if (dis[i-1][j-1] == 0) {
                dis[i][j] = 0;
            } else {
                dis[i][j] = dis[i-1][j-1] + 1;
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == 1 && j == 1) {
                dp[i][j] = 1;
            } else {
                dp[i][j] = (dp[i-1][j] + dp[i][j-1]) % mod;
                if (a[i] == b[j]) {
                    int x = i;
                    int y = j;
                    int c = 0;
                    while (dis[x][y]) {
                        int d = dis[x][y];
                        x -= d;
                        y -= d;
                        c++;
                        dp[i][j] = ((dp[i][j] - (long long) dp[x][y] * cat[c]) % mod + mod) % mod;
                    }
                }
            }
        }
    }
    cout << dp[n][n] << endl;
    return 0;
}